//////////////////////////////////////////////////////////////////////////////
//
//  Created by Philip Mulcahy on 20/01/2012.
//  Copyright (c) 2012 Philip Mulcahy. All rights reserved.
//
//  This file is part of the note-recog library.
//
//  note-recog is free software: you can redistribute it and/or modify
//  it under the terms of version 3 of the GNU Lesser General Public License 
//  as published by the Free Software Foundation.
//
//  note-recog is distributed is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with note-recog.  If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////

#ifndef note_recog_Input_h
#define note_recog_Input_h

#include <AudioToolbox/AUGraph.h>
#include <AudioUnit/AudioUnit.h>
#include <CoreAudio/CoreAudioTypes.h>
#include <AudioToolbox/AudioToolbox.h>
#include <iostream>
#include <vector>
#include "Condenser.h"
#include "FastFourierTransform.h"
#include "Log.h"

#define kInputBus 1
#define kOutputBus 0
#define kBufferSize 512
#define kBufferCount 1
#define N 10

class Listener
{
    friend OSStatus callbackWrapper(void					   *inRefCon, 
                                    AudioUnitRenderActionFlags *ioActionFlags, 
                                    const AudioTimeStamp	   *inTimeStamp, 
                                    UInt32 					   inBusNumber, 
                                    UInt32 			           inNumberFrames, 
                                    AudioBufferList            *ioData);
    
    friend std::ostream& operator<<(std::ostream& os, const Listener& in);
    
private:
    LOG_DECLARE;
    const size_t _bytesPerSample;
    const size_t _bytesPerOutputSample;
    const float _sampleRate;
    const size_t _bufferCapacity;	// In samples
    const float _freqResolution;
    const size_t _nOver2;
    double _startTime; // seconds
    std::vector<SInt16> _shortBuffer;
    std::vector<float> _floatBuffer;
    AUGraph _processingGraph;
	AudioUnit _ioUnit;
	AudioBufferList * const _bufferList;
	const AudioStreamBasicDescription _streamFormat;
	SInt16 _index;	// In samples
    unsigned long _frameCount;
    Condenser * _condenser;
    FastFourierTransform * const _fft;
    
    static Listener _instance;
    
    OSStatus callback(
                      AudioUnitRenderActionFlags 	*ioActionFlags, 
                      const AudioTimeStamp			*inTimeStamp, 
                      UInt32 						inBusNumber, 
                      UInt32 						inNumberFrames, 
                      AudioBufferList				*ioData);
    
    size_t ASBDForSoundMode();
    
    Listener();
    ~Listener();
    
public:
    static Listener& instance();
    void start(IConsumer * consumer);
    void stop();
};

std::ostream& operator<<(std::ostream& os, const Listener& in);

std::ostream& operator<<(std::ostream& os,
                         const AudioStreamBasicDescription& desc);

#endif
